import {
  Controller,
  Post,
  Body,
  Get,
  Put,
  Delete,
  UseGuards,
  Request,
  Param,
  Patch,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  /**
   * 创建一个新用户
   *
   * @param createUserDto 包含用户创建信息的DTO对象
   * @returns 返回创建的用户信息
   */
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get()
  @UseGuards(JwtAuthGuard)
  /**
   * 获取所有用户
   *
   * @returns 返回用户列表
   */
  findAll() {
    return this.userService.findAll();
  }

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Patch()
  @UseGuards(JwtAuthGuard)
  async update(@Request() req, @Body() updateUserDto: UpdateUserDto) {
    const user = await this.userService.update(req.user.sub, updateUserDto);
    return {
      code: 200,
      message: '更新成功',
      data: user,
    };
  }

  @Post('deleteAccount')
  @UseGuards(JwtAuthGuard)
  async deleteAccount(@Request() req) {
    await this.userService.remove(req.user.sub);
    return {
      code: 200,
      message: '删除成功',
    };
  }

  @Post('profile')
  @UseGuards(JwtAuthGuard)
  getProfile(@Request() req) {
    return this.userService.findOne(req.user.sub);
  } 

  @Patch('change-password')
  @UseGuards(JwtAuthGuard)
  changePassword(
    @Request() req,
    @Body() body: { oldPassword: string; newPassword: string },
  ) {
    return this.userService.changePassword(
      req.user.sub,
      body.oldPassword,
      body.newPassword,
    );
  }
}
